package com.oocl.euc.controller;

import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.oocl.euc.entity.Dish;
import com.oocl.euc.service.DishService;
import com.oocl.euc.service.MerchantService;
import com.oocl.euc.util.JSONUtil;
@Controller
public class DishController {
	@Autowired
	private MerchantService ms;
	@Autowired
	private DishService ds;
	
	@RequestMapping(value="/dishes",method=RequestMethod.GET,produces="application/json;charset=utf-8")
	@ResponseBody
	public String findDihesByMerchantId(String mid){ 
		String result="";
		List<Dish> dishes= ds.findDishesByMid(mid);
		result=JSON.toJSONString(dishes);
		return result;
	}
	
	@RequestMapping(value="/dishes",method=RequestMethod.DELETE)
	@ResponseBody
	public String deleteDish(@RequestBody String dishId){
		JSONObject dishJson=JSON.parseObject(dishId); 
		String deleteDid=dishJson.getString("did");
		ds.deleteDish(deleteDid);
		return "success";
	}
	@RequestMapping(value="/dishes",method=RequestMethod.POST)
	@ResponseBody
	public String addDish(@RequestParam String dname,@RequestParam double price,@RequestParam String mid){
		Dish d=new Dish();
		d.setDname(dname);
		d.setPrice(price); 
		
		return ds.addDish(d, mid)+"";
	}
	@RequestMapping(value="/dishes",method=RequestMethod.PUT)
	@ResponseBody
	public String updateDish(@RequestBody String dish){
		JSONObject dishJson=JSON.parseObject(dish);
		String updateDid=dishJson.getString("did"); 
		Dish d=ds.findDishByDid(updateDid);
		Double updatePrice=dishJson.getDouble("price");
		String updateDname=dishJson.getString("dname");
		d.setDname(updateDname);d.setPrice(updatePrice); 
		d.setMerchant(ds.findDishByDid(updateDid).getMerchant());
        ds.updateDish(d);
		return "success";
	}
}
